#!/bin/bash
#
# Copyright (C) 2013 Christian Babeux <christian.babeux@efficios.com>
# Copyright (C) 2014 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
#
# SPDX-License-Identifier: GPL-2.0-only

TEST_DESC="UST tracer - tracef events"

CURDIR=$(dirname $0)/
TESTDIR=$CURDIR/../..
TESTAPP_PATH="$TESTDIR/utils/testapp"
TESTAPP_NAME="gen-ust-tracef"
TESTAPP_BIN="$TESTAPP_PATH/$TESTAPP_NAME/$TESTAPP_NAME"
SESSION_NAME="tracef"
EVENT_NAME="lttng_ust_tracef:event"
NUM_TESTS=9
NR_ITER=100

source $TESTDIR/utils/utils.sh

if [ ! -x "$TESTAPP_BIN" ]; then
	BAIL_OUT "No UST nevents binary detected."
fi

function test_event_tracef()
{
	create_lttng_session_ok $SESSION_NAME $TRACE_PATH
	enable_ust_lttng_event_ok $SESSION_NAME $EVENT_NAME
	start_lttng_tracing_ok $SESSION_NAME

	$TESTAPP_BIN $NR_ITER $NR_USEC_WAIT
	ok $? "Traced application stopped."

	stop_lttng_tracing_ok $SESSION_NAME
	destroy_lttng_session_ok $SESSION_NAME

	trace_match_only $EVENT_NAME $NR_ITER $TRACE_PATH

	return $?
}

# MUST set TESTDIR before calling those functions
plan_tests $NUM_TESTS

print_test_banner "$TEST_DESC"

bail_out_if_no_babeltrace

start_lttng_sessiond

TRACE_PATH=$(mktemp -d -t tmp.test_event_tracef_trace_path.XXXXXX)
test_event_tracef
out=$?
if [ $out -ne 0 ]; then
	stop_lttng_sessiond
	exit $out
fi

stop_lttng_sessiond

rm -rf $TRACE_PATH
